// Copyright (c) 2022 NetEase, Inc. All rights reserved.
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file.

import 'package:hawk/hawk.dart';
import 'package:nim_core/nim_core.dart';

import '../main_test.dart';
import 'method_name_value.dart';

class HandleEventSubscribeCase extends HandleBaseCase {
  HandleEventSubscribeCase();

  @override
  Future<ResultBean?> handle(event) async {
    super.handle(event);

    var inputParams = Map<String, dynamic>();
    if (params != null && params!.length > 0) {
      inputParams = params![0] as Map<String, dynamic>;
    }

    switch (methodName) {
      case registerEventSubscribe:
        var publish = inputParams['publishers'] as List<dynamic>;
        List<String> publisherList = publish.map((e) => e.toString()).toList();
        EventSubscribeRequest request = EventSubscribeRequest(
            eventType: inputParams['eventType'],
            expiry: inputParams['expiry'],
            syncCurrentValue: inputParams['syncCurrentValue'],
            publishers: publisherList);
        final result = await NimCore.instance.eventSubscribeService
            .registerEventSubscribe(request);
        print(
            '=========>>eventSubscribeService:registerEventSubscribe:${result.code}：${result.errorDetails}');
        handleCaseResult = ResultBean(
            code: result.code, data: result.toMap(), message: methodName);
        break;
      case batchUnSubscribeEvent:
        var publish = inputParams['publishers'] as List<dynamic>;
        List<String> publisherList = publish.map((e) => e.toString()).toList();
        EventSubscribeRequest request = EventSubscribeRequest(
            eventType: inputParams['eventType'],
            expiry: inputParams['expiry'],
            syncCurrentValue: inputParams['syncCurrentValue'],
            publishers: publisherList);
        final result = await NimCore.instance.eventSubscribeService
            .batchUnSubscribeEvent(request);
        print(
            '=========>>eventSubscribeService:batchUnSubscribeEvent:${result.code}：${result.errorDetails}');
        handleCaseResult = ResultBean(
            code: result.code, data: result.toMap(), message: methodName);
        break;
      case unregisterEventSubscribe:
        var publish = inputParams['publishers'] as List<dynamic>;
        List<String> publisherList = publish.map((e) => e.toString()).toList();
        EventSubscribeRequest request = EventSubscribeRequest(
            eventType: inputParams['eventType'],
            expiry: inputParams['expiry'],
            syncCurrentValue: inputParams['syncCurrentValue'],
            publishers: publisherList);
        final result = await NimCore.instance.eventSubscribeService
            .unregisterEventSubscribe(request);
        print(
            '=========>>eventSubscribeService:unregisterEventSubscribe:${result.code}：${result.errorDetails}');
        handleCaseResult = ResultBean(
            code: result.code, data: result.toMap(), message: methodName);
        break;
      case publishEvent:
        Event event = Event.fromMap(inputParams);
        final result =
            await NimCore.instance.eventSubscribeService.publishEvent(event);
        print(
            '=========>>eventSubscribeService:publishEvent:${result.code}：${result.errorDetails}');
        handleCaseResult = ResultBean(
            code: result.code, data: result.toMap(), message: methodName);
        break;
      case querySubscribeEvent:
        var publish = inputParams['publishers'] as List<dynamic>;
        List<String> publisherList = publish.map((e) => e.toString()).toList();
        EventSubscribeRequest request = EventSubscribeRequest(
            eventType: inputParams['eventType'],
            expiry: inputParams['expiry'],
            syncCurrentValue: inputParams['syncCurrentValue'],
            publishers: publisherList);
        final result = await NimCore.instance.eventSubscribeService
            .querySubscribeEvent(request);
        print(
            '=========>>eventSubscribeService:querySubscribeEvent:${result.code}：${result.errorDetails}');
        handleCaseResult = ResultBean(
            code: result.code, data: eventToMap(result), message: methodName);
        break;
      case eventSubscribeStream:
        subsriptions.add(NimCore
            .instance.eventSubscribeService.eventSubscribeStream
            .listen((event) {
          print(
              '=========>>eventSubscribeService:eventSubscribeStream:${event.map((e) => e.toMap())}');
          IntegratedManager.instance.report('eventSubscribeStream',
              ResultBean(code: 0, data: event.map((e) => e.toMap()).toList()));
        }));
        handleCaseResult = ResultBean.success(data: null);
        break;
    }

    return handleCaseResult;
  }

  Map<String, dynamic> eventToMap(NIMResult result) {
    var dataMap = null;
    if (result.data is List<EventSubscribeResult>) {
      var tempData = result.data
          ?.map((e) => <String, dynamic>{
                'eventType': e.eventType,
                'expiry': e.expiry,
                'time': e.time,
                'publisherAccount': e.publisherAccount,
              })
          ?.toList();
      dataMap = <String, dynamic>{'eventSubscribeResultList': tempData};
    }
    return {
      'code': result.code,
      'data': dataMap ?? result.data,
      'msg': result.errorDetails,
    };
  }
}
